home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1385 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: cnn.Princeton.EDU!franck!tim
  2. From: tim@franck.Princeton.EDU (Tim Hollebeek)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: size of vtable
  5. Date: 10 Jan 1996 19:17:15 GMT
  6. Organization: Princeton University
  7. Message-ID: <4d13br$9jf@cnn.Princeton.EDU>
  8. References: <4cr1ob$1cu@seal.abalon.se> <30f2dee6.0@news.sbbs.se>
  9. NNTP-Posting-Host: franck.princeton.edu
  10.  
  11. In article <30f2dee6.0@news.sbbs.se>, Urban Fosseus <ufo@sbbs.se> wrote:
  12. >In article <4cr1ob$1cu@seal.abalon.se>, ken1@abalon.se says...
  13. >>
  14. >>Is it possible to know the size of the vtbl for a given class 
  15. >>at compile time (to do something like
  16. >>
  17. >>int x = sizeof(theclass::vtbl)
  18. >>
  19. >>or similar)?
  20.  
  21. First, the obvious: Why do you care?  Ignoring that ...
  22.  
  23. >If you have control over the base class, you can
  24. >evaluate the offset of the first data member with code like
  25. >
  26. >int sz_vtab = ((int)(&((myclass *)1)->data)-1);
  27.  
  28. Many compilers put the vtable before the pointer, for efficiency; your
  29. hack would return zero on such implementations.  Even without that
  30. problem, your code returns the size of the vtable _pointer_ not the
  31. table, which is a far less useful; probably even a constant
  32. (sizeof(void *)).  Did you actually test this before posting?
  33.  
  34. >Both approaches might fail if the compiler aligns data members and
  35. >rounds struct/class sizes in unpredicted ways. Strictly speaking,
  36. >the vtable itself is not required by the standard, but its effects.
  37. >So this is not strictly portable code, but code that you can prove
  38. >works for all interesting platforms ;-)
  39.  
  40. On a SGI using g++, your 'working' code evaluates to zero, independent
  41. of vtable size.  Since SGIs are interesting platforms, and g++ is an
  42. extremely common compiler, I conclude you haven't a clue what you are
  43. talking about.
  44.  
  45. Not all interesting platforms use vtables, by a long shot.  As shown
  46. above, your code will not do anything useful on _any_ platform, which
  47. leads me to believe you wrote your article off the top of your head.
  48.  
  49. Please don't post useless garbage.
  50.  
  51. -- 
  52. Tim Hollebeek      | Everything above is a true statement, for sufficiently
  53. PChem Grad Student | false values of true.
  54. Princeton Univ.    | tim@wfn-shop.princeton.edu
  55. -------------------| http://wfn-shop.princeton.edu/~tim
  56.